package com.mbyte.easy.rest.tStudent;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.studentInfo.entity.TStudent;
import com.mbyte.easy.studentInfo.service.ITStudentService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/tStudent")
public class RestTStudentController extends BaseController  {

    @Autowired
    private ITStudentService tStudentService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param tStudent
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, TStudent tStudent) {
        Page<TStudent> page = new Page<TStudent>(pageNo, pageSize);
        QueryWrapper<TStudent> queryWrapper = new QueryWrapper<TStudent>();

        if(tStudent.getUserId() != null  && !"".equals(tStudent.getUserId() + "")) {
            queryWrapper = queryWrapper.like("user_id",tStudent.getUserId());
         }


        if(tStudent.getClassId() != null  && !"".equals(tStudent.getClassId() + "")) {
            queryWrapper = queryWrapper.like("class_id",tStudent.getClassId());
         }


        if(tStudent.getName() != null  && !"".equals(tStudent.getName() + "")) {
            queryWrapper = queryWrapper.like("name",tStudent.getName());
         }


        if(tStudent.getSex() != null  && !"".equals(tStudent.getSex() + "")) {
            queryWrapper = queryWrapper.like("sex",tStudent.getSex());
         }


        if(tStudent.getAge() != null  && !"".equals(tStudent.getAge() + "")) {
            queryWrapper = queryWrapper.like("age",tStudent.getAge());
         }


        if(tStudent.getPhone() != null  && !"".equals(tStudent.getPhone() + "")) {
            queryWrapper = queryWrapper.like("phone",tStudent.getPhone());
         }


        if(tStudent.getEmail() != null  && !"".equals(tStudent.getEmail() + "")) {
            queryWrapper = queryWrapper.like("email",tStudent.getEmail());
         }

        IPage<TStudent> pageInfo = tStudentService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  tStudent);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param tStudent
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(TStudent tStudent){
        return toAjax(tStudentService.save(tStudent));
    }

    /**
    * 添加
    * @param tStudent
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(TStudent tStudent){
        return toAjax(tStudentService.updateById(tStudent));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(tStudentService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(tStudentService.removeByIds(ids));
    }

}

